package com.bl._03oneway;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 *  消费者
 */
public class Consumer {

    public static void main(String[] args) throws MQClientException {
        // 1 创建消费者对象
        DefaultMQPushConsumer pushConsumer = new DefaultMQPushConsumer("oneway_hello_group");
        // 2.连接mq
        pushConsumer.setNamesrvAddr("localhost:9876");

        // 4.消费者订阅主题，表示此消费者消费哪个主题的消息，二级分类可以是*
        pushConsumer.subscribe("oneway","*");

        // 5.注册监听器
        pushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {

                    System.out.println(new String(messageExt.getBody(), StandardCharsets.UTF_8));

                }
                // 标记该消息已经被成功消费
                return  ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        // 3.启动消费者
        pushConsumer.start();
    }
}
